
#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"
std::vector<std::vector<int>> multiple(
        std::vector<std::vector<int>> a,
        std::vector<std::vector<int>> b){
    auto c=a;
    for(int i=0;i<a.size();i++){//行
        for(int j=0;j<a.size();j++){//列
            int sum=0;
            for(int k=0;k<a.size();k++){
                sum+=a[k][i]*b[j][k];
            }
            c[j][i]=sum;
//            a[i][j]=a[i][j]*b[j][i];
        }
    }
    return c;
}
class OneLoop{
public:
    std::vector<std::vector<int>> vec2;
    void run(int n,int m){
        for(int i=0;i<n;i++){
            std::vector<int> vec;
            for(int j=0;j<n;j++){
                int a;
                std::cin>>a;
                vec.push_back(a);
            }
            vec2.push_back(vec);
        }
        auto vec3=vec2;
        if(m==0){
            for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    if(j!=0){
                        printf(" ");
                    }
                    printf("%d",i==j?1:0);
                }
                printf("\n");
            }
            return;
        }
        m--;
        while(m){
            vec2= multiple(vec2,vec3);
            m--;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(j!=0){
                    printf(" ");
                }
                printf("%d",vec2[i][j]);
            }
            printf("\n");
        }
    }
};
class Solution {
public:
    void run() {

        bool first = true;
        int a,b,c,d,e;

        while (std::cin>>a>>b)
        {
            OneLoop loop;
            loop.run(a,b);
        }
    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

